import * as moment from 'moment';
export class ProgressModel {
    public ganttName = '';
    private _startDate: any = null;

    public get startDate() {
        return this._startDate;
    }

    public set startDate(value) {
        if (moment(value).isAfter(moment(this._endDate))) {
            this._endDate = value;
        }
        this._startDate = value;
    }


    private _endDate: any = null;
    public get endDate() {
        return this._endDate;
    }
    public set endDate(value) {
        if (moment(value).isBefore(moment(this._startDate))) {
            this._startDate = value;
        }
        this._endDate = value;
    }
    public calendar: any;
    public exceptDate: any = [];
    public prevGanttList: any = [];
    public parentId: number = null;
    public tips = '';
    public step: number | string = 0;
    public id: number = null;
    public projectId: number | string = null;
    public isEdit = false;
    public constructor(param?) {
        if (!param) {
            return;
        }
        (param.isEdit) && (this.isEdit = param.isEdit);
        (param.ganttName) && (this.ganttName = param.ganttName);
        (param.startDate) && (this.startDate = param.startDate);
        (param.endDate) && (this.endDate = param.endDate);
        (param.calendar) && (this.calendar = param.calendar);
        (param.exceptDate) && (this.exceptDate = param.exceptDate);
        (param.prevGanttList) && (this.prevGanttList = param.prevGanttList);
        (param.parentId) && (this.parentId = param.parentId);
        (param.tips) && (this.tips = param.tips);
        (param.step) && (this.step = param.step);
        (param.id) && (this.id = param.id);
        (param.projectId) && (this.projectId = param.projectId);
    }

    public toCreateJson() {
        return {
            projectId: this.projectId,
            ganttName: this.ganttName,
            step: this.step,
            calendar: this.dealWithCalendar(),
            startAt: moment(this.startDate).unix(),
            finishAt: moment(this.startDate).unix(),
            parentId: 0,
            tips: this.tips
        };
    }

    public dealWithCalendar() {
        let exceptDate = this.exceptDate;
        let newDate = [];
        for (let i = 0; i < exceptDate.length; i++) {
            let element = exceptDate[i];
            if (element.name !== '' && element.startDate && element.endDate) {
                newDate.push({
                    name: element.name,
                    startDate: moment(element.startDate).unix(),
                    endDate: moment(element.endDate).unix()
                });
            }
        }
        this.calendar = {
            exceptDate: newDate
        };
        return JSON.stringify(this.calendar);
    }

    public updateName() {
        return {
            id: this.id,
            step: this.step,
            ganttName: this.ganttName,
            projectId: this.projectId
        };
    }
    public updateCalendar() {
        return {
            id: this.id,
            step: this.step,
            calendar: this.dealWithCalendar(),
            projectId: this.projectId
        };
    }
}
